#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#define dimNum	5000*1000
#define factNum	5000*1000 * 8
#define RATE	0.1

int main(void){
	FILE *fact, *dim ;
	int i,j;
	int key,value;
	int *pos;
	int k=0;
	dim = fopen("./dimTableText","w");
	for(i=0;i<dimNum;i++){
		srandom(i*i+1);
		value = random();
		fprintf(dim,"%d\t%d\n",i,value);
	}
	srandom(0);

	pos = (int *) malloc(sizeof(int) * factNum);
	if(!pos){
		printf("failed to allocate memory\n");
		exit(-1);
	}
	memset(pos, 0, sizeof(int)* factNum);

	for(i=0;;i++){
		key = abs(random()) %(factNum);
		if(pos[key] ==0 ){
			k++;
			pos[key] = 1;
			if(k == factNum*RATE)
				break;
		}
	}

	fact = fopen("./factTableText","w");
	for(i=0;i<factNum;i++){
		srandom(i*11 +1);
		if(pos[i] == 1){
			key = abs(random()) % (dimNum);
			k++;
		}else
			key = random() % (dimNum) + dimNum;
		value = random();
		fprintf(fact,"%d\t%d\n",key,value);
	}
	fclose(fact);
	fclose(dim);
	free(pos);
	return 0;
}
